/*
 * p1185.cpp
 *
 *  Created on: 2013-7-30
 *      Author: zy
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
int f[2][65][65], cnt, ans, n, m;
int Map[101];
int s[65], c[65];
bool fit(int x)
{
	if (x & (x << 1))
		return false;
	if (x & (x << 2))
		return false;
	return true;
}
int count(int k)
{
	int ans = 0;
	while (k > 0)
	{
		ans += k % 2;
		k /= 2;
	}
	return ans;
}
void findState()
{
	cnt = 0;
	for (int i = 0; i < (1 << m); i++)
		if (fit(i))
		{
			cnt++;
			s[cnt] = i;
			c[cnt] = count(i);
		}
}
void init()
{
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
	{
		char tem[15];
		scanf("%s", tem + 1);
		int state = 0;
		for (int j = 1; j <= m; j++)
			if (tem[j] == 'P')
				state = state * 2;
			else
				state = state * 2 + 1;
		Map[i] = state;
	}
	Map[0] = 0;
}
void Dp()
{
	memset(f, -1, sizeof(f));
	int ans = 0;
	for (int s1 = 1; s1 <= cnt; s1++)
		if ((s[s1] & Map[1]) == 0)
			f[1][s1][1] = c[s1];

	for (int i = 2; i <= n; i++)
	{
		memset(f[i & 1], -1, sizeof(f[i & 1]));
		for (int s1 = 1; s1 <= cnt; s1++)
		{
			if (s[s1] & Map[i])
				continue;
			for (int s2 = 1; s2 <= cnt; s2++)
			{
				if (s[s1] & s[s2])
					continue;
				for (int s3 = 1; s3 <= cnt; s3++)
				{
					if (s[s1] & s[s3])
						continue;
					if (s[s2] & s[s3])
						continue;
					if (f[1 - i & 1][s2][s3] == -1)
						continue;
					f[i & 1][s1][s2] = max(f[i & 1][s1][s2],
							f[1 - i & 1][s2][s3] + c[s1]);
				}
			}

		}
	}
}
void print()
{
	int ans = 0;
	for (int i = 1; i <= cnt; i++)
		for (int j = 1; j <= cnt; j++)
			ans = max(ans, f[n & 1][i][j]);
	printf("%d\n", ans);
}
int main()
{
	init();
	findState();
	Dp();
	print();
	return 0;
}
